/*
 * drvCore.java
 *
 * Common definitions for the communication between xAP driver and VM elements
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 07/11/09 by Daniel Berenguer : first version
 */

package xAP;


import VisualLogic.MyOpenLabDriverOwnerIF;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Vector;


public class drvCore
{
    // xAP UDP socket
    public static DatagramSocket socket;
    // xAP broadcast address
    public static String xAP_BCastAddr = "255.255.255.255";
    // xAP loopback address    
    public static String xAP_LBackAddr = "127.0.0.1";
    // Default xAP UDP port
    public static int xAP_DefPort = 3639;
    // Min UDP port
    public static int xAP_MinPort = 49152;
    // Max UDP port
    public static int xAP_MaxPort = 65000;
    // Actual xAP UDP port
    public static int xAP_Port = 0;

    // Element-driver messages
    public static String DRVCOMM_RECV_BSCVAL = "recv_val";      // Receive BSC value from driver
    public static String DRVCOMM_REQU_BSCVAL = "requ_val";      // Request BSC value from VM element

    // Version of xAP spec
    public static short xAP_Version = 13;
    // MyOpenLab xAP UID
    public static String xAP_BaseUID = "FF0165";
    // MyOpenLab xAP source address
    public static String xAP_AddressBase = "opnode.openlab";
    public static String xAP_SourceAddr = "opnode.openlab.myopenlab1";
    // General xAP enable flag
    public static boolean xAP_Run = false;

    // Vector of xAP devices
    public static xAPdevBSCtable xAPdevices;
    // Vector of BSC endpoints
    public static xAPBSCtable bscEndpoints;
    // Vector of xAP VM elements
    public static Vector vXAPelements;


    // Element iterator
    private static int elementIndex = 0;

   /********************************************************************************************
    * send_xAPmessage
    *
    * Send xAP message
    *
    * 'message'     Message string to send
    *
    ********************************************************************************************/
    public static void send_xAPmessage(String message) throws xAPexception
    {
        DatagramPacket packet;

        try
        {
            packet = new DatagramPacket(message.getBytes(), message.length(), InetAddress.getByName(xAP_BCastAddr), xAP_DefPort);

            // Send message
            try
            {
                socket.send(packet);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

   /********************************************************************************************
    * SendValue
    *
    * Send value to any VM element with a given UID
    *
    * 'Source'          xAP source address
    * 'value'           Value to send
    *
    ********************************************************************************************/
    public static void SendValue(String Source, Object value)
    {
        xAPelement epElement;
        int i;

        for(i=0 ; i<vXAPelements.size() ; i++)
        {
            epElement = (xAPelement)vXAPelements.elementAt(i);

            if (epElement.getSource() != null)
            {
                if (epElement.getSource().compareToIgnoreCase(Source) == 0)
                {
                    // The xAP element is an input
                    if (epElement.isOutput() == false)
                    {
                        epElement.getOwner().getCommand(DRVCOMM_RECV_BSCVAL, value);
                    }
                }
            }
        }
    }

   /********************************************************************************************
    * AddElementToTable
    *
    * Add xAP element into the static table
    *
    * 'xElement'       xAP element
    *
    ********************************************************************************************/
    public static void AddElementToTable(xAPelement xElement)
    {
        vXAPelements.addElement(xElement);
    }

   /********************************************************************************************
    * getElementFromOwner
    *
    * Return element from a given Driver owner
    *
    * 'owner'       Driver owner handler
    *
    * Returns:
    *   xAP element
    ********************************************************************************************/
    public static xAPelement getElementFromOwner(MyOpenLabDriverOwnerIF owner)
    {
        xAPelement epElement;
        int i;

        for(i=0 ; i<vXAPelements.size() ; i++)
        {
            epElement = (xAPelement)vXAPelements.elementAt(i);

            if (epElement.getOwner().equals(owner))
                return epElement;
        }

        return null;
    }

   /********************************************************************************************
    * getFirstElementFromSource
    *
    * Return first element from a given xAP Source address
    *
    * 'Source'       xAP Source address
    *
    * Returns:
    *   xAP element
    ********************************************************************************************/
    public static xAPelement getFirstElementFromSource(String Source)
    {
        xAPelement epElement;
        int i;

        for(i=0 ; i<vXAPelements.size() ; i++)
        {
            epElement = (xAPelement)vXAPelements.elementAt(i);

            if (epElement.getSource().equals(Source))
            {
                elementIndex = i;
                return epElement;
            }
        }

        elementIndex = 0;
        return null;
    }

   /********************************************************************************************
    * getNextElementFromSource
    *
    * Return next element from a given xAP Source address
    *
    * 'Source'       xAP Source address
    *
    * Returns:
    *   xAP element
    ********************************************************************************************/
    public static xAPelement getNextElementFromSource(String Source)
    {
        xAPelement epElement;
        int i, initIndex = elementIndex + 1;

        if (initIndex < vXAPelements.size())
        {
            for(i=elementIndex+1 ; i<vXAPelements.size() ; i++)
            {
                epElement = (xAPelement)vXAPelements.elementAt(i);

                if (epElement.getSource().equals(Source))
                {
                    elementIndex = i;
                    return epElement;
                }
            }
        }

        elementIndex = 0;
        return null;
    }
}

